//
//  AppDelegate.swift
//  sinaWeibo
//
//  Created by Beans on 16/6/23.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.rootViewController = rootVc()   // 根据是否登录, 跳转到不同的控制器
        window?.makeKeyAndVisible()

        // MARK: - 视图切换控制 (多对一, 通知)
        // 1. 注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "enter:", name: kNotificationChangeRootVc, object: nil)

        setupAppreance()

        return true
    }

    // 实现方法
    @objc private func enter(noti: NSNotification) {    // 忘了写`@objc`, 报错 (外部vc调用不了). <按钮/监听事件等运行时的交互>

        // 先可选转成 HBOauthVc
        if let vc = noti.object as? UIViewController {

            // 需要判断是不是HBOauthVc.
            if vc is HBOauthViewController {
                window?.rootViewController = HBWelcomeViewController()
            } else {
                window?.rootViewController = HBTabBarController()
            }
        }
    }

    // MARK: - 3. 移除通知
    // 此处AppDelegate不会走, 习惯
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    private func rootVc() -> UIViewController {
        return HBOauthViewModel.shareInstance.isLogin ? (HBWelcomeViewController()) : (HBTabBarController())
    }

    private func setupAppreance() {
        // 设置UINavigationBar的 tintColor
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()

        // 设置tabBar的 tintColor
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }

}

